package com.knowif.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.knowif.dao.UserRedpacketMapper;
import com.knowif.pojo.UserRedpacket;
import com.knowif.pojo.UserRedpacketExample;
import com.knowif.util.commonUtil.IntegerUtil;

@Service
public class UserRedpacket_Service {
	
	@Autowired
	private UserRedpacketMapper userRedpacketMapper;
	
	public List<UserRedpacket> selectByUserIdAndActivityId(Integer userId,Integer activityId) {
		if(IntegerUtil.isEmpty(userId) || IntegerUtil.isEmpty(activityId)){
			return new ArrayList<UserRedpacket>();
		}
		UserRedpacketExample userRedpacketExample = new UserRedpacketExample();
		userRedpacketExample.createCriteria().andUserIdEqualTo(userId).andActivityIdEqualTo(activityId);
		List<UserRedpacket> userRedpackets = userRedpacketMapper.selectByExample(userRedpacketExample);
		return userRedpackets;
	}
	/**
	 * @author: xingyao 
	 * @Description:  查找用户创建的红包分享
	 * @param userRedpacketExample
	 * @return
	 */
	public List<UserRedpacket> selectByExample(UserRedpacketExample redpacketExample){
		if(redpacketExample == null){
			return new ArrayList<UserRedpacket>();
		}else{
			return userRedpacketMapper.selectByExample(redpacketExample);
		}
	}
	
	
	
	
	public int insert(UserRedpacket userRedpacket){
		if(userRedpacket == null){
			return 0;
		}
		return userRedpacketMapper.insert(userRedpacket);
	}
	
	
	public int updateById(UserRedpacket userRedpacket){
		if(userRedpacket == null){
			return 0;
		}
		return userRedpacketMapper.updateByPrimaryKey(userRedpacket);
	}
	
	public UserRedpacket selectById(Integer id){
		if(IntegerUtil.isEmpty(id)){
			return null;
		}
		return userRedpacketMapper.selectByPrimaryKey(id);
	}
	
	
	
	public int update(Integer number,int activityId,int userId,UserRedpacket userRedpacket){
		if(IntegerUtil.isEmpty(number)|| IntegerUtil.isEmpty(activityId)||IntegerUtil.isEmpty(userId)){
			return 0;
		}
		UserRedpacketExample userRedpacketExample = new UserRedpacketExample();
		//应用乐观锁，用分享数量进行判断更新
		userRedpacketExample.createCriteria().andSharedNumEqualTo(number)
		.andActivityIdEqualTo(activityId)
		.andActivityIdEqualTo(userId);
		return userRedpacketMapper.updateByExample(userRedpacket, userRedpacketExample);
	}
	
	
	
	
	
	


}
